#pragma once

#include "GameMap.h"
#include "BeanieCore.h"
#include "FileParse.h"
#include "FolderParse.h"
#include "Graph.h"

#define SAFE_RELEASE_TVECTOR_POINTER(x) for(int i = 0; i < x.size(); i++)\
							 {delete x[i];}x.release();

class MapManager
{
private:
	TemplateVector<map*> m_maps;
	BeanieCore* m_core;
	FileGetter m_folder;
	FileParser m_parser;
	TemplateVector<BeanieTexture> textures;
	Graph<mapNode> graph;
	TemplateVector<GraphNode<mapNode>*> m_nodes;
	int currentMap;

	void fillGraph()
	{
		for (int i = 0; i < m_maps[currentMap]->tileset.size(); i++)
		{
			for (int j = 0; j < strlen(m_maps[currentMap]->tileset[0]); j++)
			{
				GraphNode<mapNode>* a_node;
				a_node = new GraphNode<mapNode>;
				mapNode newNode;
				newNode.m_id = i+j;
				newNode.pos = V2DI(j,i);
				a_node->value = newNode;
				m_nodes.add(a_node);
			}
		}
	}

	void fillConnections(){
		int length = m_nodes.size();
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < length; j++)
			{
				if(((m_nodes[i]->value.pos.y == m_nodes[j]->value.pos.y-1)
					&&(m_nodes[i]->value.pos.x == m_nodes[j]->value.pos.x))||
					((m_nodes[i]->value.pos.y == m_nodes[j]->value.pos.y+1)
					&&(m_nodes[i]->value.pos.x == m_nodes[j]->value.pos.x)))
				{
					graph.connectTwoWay(m_nodes[i],m_nodes[j]);
				}
				else if(((m_nodes[i]->value.pos.x == m_nodes[j]->value.pos.x-1)
					&&(m_nodes[i]->value.pos.y == m_nodes[j]->value.pos.y))||
					((m_nodes[i]->value.pos.x == m_nodes[j]->value.pos.x+1)
					&&(m_nodes[i]->value.pos.y == m_nodes[j]->value.pos.y)))
				{
					graph.connectTwoWay(m_nodes[i],m_nodes[j]);
				}
			}
		}
	}

public:
	MapManager(){}
	~MapManager(){release();}
	void release()
	{
		if(m_maps.size() != 0)m_maps[0]->release();
		SAFE_RELEASE_TVECTOR_POINTER(m_maps);
		SAFE_RELEASE_TVECTOR_POINTER(m_nodes);
	}
	int init(BeanieCore* a_core)
	{
		m_core = a_core;
		TemplateVector<char*> files;
		m_folder.sortFolder(files,"maps\\","*.map",true);
		for (int i = 0; i < files.size(); i++)
		{
			m_maps.add(new map());
			m_parser.printFileToVector(m_maps[i]->tileset,'/',files[i]);
		}
		SAFE_RELEASE_TVECTOR_POINTER(files);
		loadTextures();
		return 0;
	}

	void loadTextures()
	{
		BeanieTexture temp, temp2;
		m_core->createTexture(&temp,"textures/Floor.jpg");
		textures.add(temp);
		m_core->createTexture(&temp2,"textures/Wall.jpg");
		textures.add(temp2);
	}

	void render(int level){
		int mapy = m_maps[level]->tileset.size();
		int mapx = strlen(m_maps[level]->tileset[0]);

		for (int i = 0; i < mapy; i++)
		{
			for (int j = 0; j < mapx; j++)
			{
				if(m_maps[level]->tileset[i][j] == '#')
				{
					m_core->updateTransform(&textures[1],
							V2DF((float)j*64,(float)i*64),V2DF(.5f,.5f),0);
					m_core->drawTexture(&textures[1]);
				}
				if(m_maps[level]->tileset[i][j] == '.'||m_maps[level]->tileset[i][j] == '&'||m_maps[level]->tileset[i][j] == '!')
				{
					m_core->updateTransform(&textures[0],
							V2DF((float)j*64,(float)i*64),V2DF(.5f,.5f),0);
					m_core->drawTexture(&textures[0]);
				}
			}
		}
	
	}
	void update(){}

};